package com.forum.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.forum.annotation.IgnoreAuth;
import com.forum.entity.ExamrecordtabEntity;
import com.forum.entity.view.ExamrecordtabView;
import com.forum.service.ExamrecordtabService;
import com.forum.utils.MPUtil;
import com.forum.utils.PageUtils;
import com.forum.utils.R;
import io.swagger.annotations.Api;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 *  考试记录
 * </p>
 *
 * @author 张继成
 * @since 2023-12-31
 */
@RestController
@RequestMapping("/examrecord")
@Api(tags = "考试记录控制")
public class ExamrecordtabController {
    @Autowired
    private ExamrecordtabService examrecordService;

    /**
     * 考试记录接口
     */
    @RequestMapping("/groupby")
    @SuppressWarnings("unchecked")
    public R page2(@RequestParam Map<String, Object> params, ExamrecordtabEntity examrecord, @NotNull HttpServletRequest request){
        if(!request.getSession().getAttribute("role").toString().equals("管理员")) {
            examrecord.setUserid((Long)request.getSession().getAttribute("userId"));
        }

        EntityWrapper<ExamrecordtabEntity> ew = new EntityWrapper<ExamrecordtabEntity>();
        PageUtils page = examrecordService.queryPageGroupBy(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, examrecord), params), params));
        return R.ok().put("data", page);
    }

    /**
     * 后端列表
     */
    @RequestMapping("/page")
    @SuppressWarnings("unchecked")
    public R page(@RequestParam Map<String, Object> params, ExamrecordtabEntity examrecord,
                  HttpServletRequest request){
        if(!request.getSession().getAttribute("role").toString().equals("管理员")) {
            examrecord.setUserid((Long)request.getSession().getAttribute("userId"));
        }
        EntityWrapper<ExamrecordtabEntity> ew = new EntityWrapper<ExamrecordtabEntity>();

        PageUtils page = examrecordService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, examrecord), params), params));

        return R.ok().put("data", page);
    }

    /**
     * 前端列表
     */
    @RequestMapping("/list")
    @SuppressWarnings("unchecked")
    public R list(@RequestParam Map<String, Object> params, ExamrecordtabEntity examrecord,
                  HttpServletRequest request){
        if(!request.getSession().getAttribute("role").toString().equals("管理员")) {
            examrecord.setUserid((Long)request.getSession().getAttribute("userId"));
        }
        EntityWrapper<ExamrecordtabEntity> ew = new EntityWrapper<ExamrecordtabEntity>();

        PageUtils page = examrecordService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, examrecord), params), params));
        return R.ok().put("data", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/lists")
    @SuppressWarnings("unchecked")
    public R list(ExamrecordtabEntity examrecord){
        EntityWrapper<ExamrecordtabEntity> ew = new EntityWrapper<ExamrecordtabEntity>();
        ew.allEq(MPUtil.allEQMapPre( examrecord, "examrecord"));
        return R.ok().put("data", examrecordService.selectListView(ew));
    }

    /**
     * 查询
     */
    @RequestMapping("/query")
    @SuppressWarnings("unchecked")
    public R query(ExamrecordtabEntity examrecord){
        EntityWrapper< ExamrecordtabEntity> ew = new EntityWrapper< ExamrecordtabEntity>();
        ew.allEq(MPUtil.allEQMapPre( examrecord, "examrecord"));
        ExamrecordtabView examrecordView =  examrecordService.selectView(ew);
        return R.ok("查询考试记录表成功").put("data", examrecordView);
    }

    /**
     * 后端详情
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        ExamrecordtabEntity examrecord = examrecordService.selectById(id);
        return R.ok().put("data", examrecord);
    }

    /**
     * 前端详情
     */
    @IgnoreAuth
    @RequestMapping("/detail/{id}")
    public R detail(@PathVariable("id") Long id){
        ExamrecordtabEntity examrecord = examrecordService.selectById(id);
        return R.ok().put("data", examrecord);
    }




    /**
     * 后端保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody ExamrecordtabEntity examrecord, HttpServletRequest request){
        examrecord.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue());
        //ValidatorUtils.validateEntity(examrecord);
        examrecord.setUserid((Long)request.getSession().getAttribute("userId"));
        examrecordService.insert(examrecord);
        return R.ok();
    }

    /**
     * 前端保存
     */
    @RequestMapping("/add")
    public R add(@RequestBody ExamrecordtabEntity examrecord, HttpServletRequest request){
        examrecord.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue());
        //ValidatorUtils.validateEntity(examrecord);
        examrecord.setUserid((Long)request.getSession().getAttribute("userId"));
        examrecordService.insert(examrecord);
        return R.ok();
    }



    /**
     * 修改
     */
    @RequestMapping("/update")
    @Transactional
    public R update(@RequestBody ExamrecordtabEntity examrecord, HttpServletRequest request){
        //ValidatorUtils.validateEntity(examrecord);
        examrecordService.updateById(examrecord);//全部更新
        return R.ok();
    }




    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
        examrecordService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }





    /**
     * 当重新考试时，删除考生的某个试卷的所有考试记录
     */
    @RequestMapping("/deleteRecords")
    public R deleteRecords(@RequestParam Long userid, @RequestParam Long paperid){
        examrecordService.delete(new EntityWrapper<ExamrecordtabEntity>().eq("paperid", paperid).eq("userid", userid));
        return R.ok();
    }
}
